package com.gxa.ssm251.controller;

import com.gxa.ssm251.exception.BusinessException;
import com.gxa.ssm251.pojo.Coupon;
import com.gxa.ssm251.pojo.CpScope;
import com.gxa.ssm251.service.CouponService;
import com.gxa.ssm251.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 舒空
 * //@CrossOrigin 解决跨域
 */
@RestController
@RequestMapping("/coupon")
@Api(value = "优惠券控制器", tags = "优惠券接口api")
@CrossOrigin//解决跨域

public class CouponController {

    @Resource
    private CouponService couponService;

    private final JsonResult jsonResult = new JsonResult();

    @ApiOperation(value = "修改优惠券是否可用")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "coupon",value = "json格式的优惠券对象",paramType = "body")
    )
    @PostMapping("/updateInUse")
    public JsonResult updateInUse(@RequestBody Coupon coupon) {

        Boolean updateInUse = couponService.updateInUse(coupon);

        if (updateInUse){
            jsonResult.setCode("1");
            jsonResult.setMsg("优惠券已调至可用状态");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("优惠券已调至不可用状态");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改优惠券")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "map",value = "json格式的优惠券对象",paramType = "body")
    )
    @PostMapping("/update")
    public JsonResult updateCoupon(@RequestBody Map<String, Object> map) {
        Integer cpId = Integer.valueOf(map.get("id").toString());
        List<Coupon> selectById = couponService.selectById(cpId);
        if (selectById!=null){
            jsonResult.setData(selectById);
        }

        Coupon coupon = new Coupon();
        coupon.setId(cpId);
        coupon.setName(map.get("name").toString());
        coupon.setIssuing(map.get("issuing").toString());
        coupon.setItRvl(map.get("itRvl").toString());
        coupon.setStartTime(Date.valueOf(map.get("startTime").toString()));
        coupon.setEndTime(Date.valueOf(map.get("endTime").toString()));
        coupon.setNumber(Integer.valueOf(map.get("number").toString()));
        coupon.setPrice(Double.valueOf(map.get("price").toString()));
        coupon.setSuperposition(Boolean.valueOf(map.get("superposition").toString()));
        System.out.println("---------------"+coupon.toString());
        Integer update = couponService.update(coupon);

        String scope = map.get("Scope").toString();
        List<String> list = new ArrayList<>();
        String[] split = scope.split(",");
        list= Arrays.asList(split);
        List<Integer> aList = new ArrayList<>();
        List<Integer> bList = new ArrayList<>();
        Integer i=0;
        Integer cpScopeId=0;
        for (String s : list) {
             i = Integer.parseInt(s);
             aList.add(i);
        }
        for (Coupon coupon1 : selectById) {
            for (CpScope cpScope : coupon1.getScopeList()) {
                cpScopeId = cpScope.getId();
                bList.add(cpScopeId);
            }
        }
        Integer updateScope=0;
        /*System.out.println("aList:"+aList);
        System.out.println("bList:"+bList);
        System.out.println("!aList.equals(bList):------"+!aList.equals(bList));*/

        //误差解决如果使用范围未改变就不改变updateScope的值从而不影响返回修改的msg
        if (!aList.equals(bList)){
            updateScope  = couponService.updateScope(cpId, list);
        }



        if (update>0||updateScope>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页逆序全查询接口")
    @ApiImplicitParams(
            @ApiImplicitParam(
                    name = "map", value = "json格式的优惠券对象", paramType = "body"
            )
    )
    @PostMapping("/selectAllByPage")
    public JsonResult selectAllByPage(@RequestBody Map<String, Object> map) {
        Integer page = Integer.parseInt(map.get("page").toString());
        Integer size = Integer.parseInt(map.get("size").toString());
        List<Coupon> coupons = couponService.selectAllByPage(page, size);
        Integer selectCountAll = couponService.selectCountAll();
        if (coupons != null) {
            jsonResult.setCode("1");
            jsonResult.setCount(selectCountAll);
            jsonResult.setData(coupons);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("数据还在加载中...");
        }
        return jsonResult;
    }


    @ApiOperation(value = "优惠券添加接口")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "map", value = "json格式的优惠券对象",
                    paramType = "body")
    )
    @PostMapping("/add")
    public JsonResult addCoupon(@RequestBody Map<String, Object> map) {

        Coupon coupon = new Coupon();
        coupon.setName(map.get("name").toString());
        coupon.setIssuing(map.get("issuing").toString());
        coupon.setItRvl(map.get("itRvl").toString());
        coupon.setStartTime(Date.valueOf(map.get("startTime").toString()));
        coupon.setEndTime(Date.valueOf(map.get("endTime").toString()));
        coupon.setNumber(Integer.valueOf(map.get("number").toString()));
        coupon.setPrice(Double.valueOf(map.get("price").toString()));
        coupon.setSuperposition(Boolean.valueOf(map.get("superposition").toString()));

        Integer integer = null;
        try {
            integer = couponService.addCoupon(coupon);
            Coupon selectByName = couponService.selectByName(coupon.getName());
            System.out.println("1.name:" + coupon.getName());
            String scope = map.get("Scope").toString();
            System.out.println("2.scope" + scope);
            List<String> list = new ArrayList<>();
            String[] split = scope.split(",");
            System.out.println("3.split:" + Arrays.toString(split));
            list = Arrays.asList(split);
            System.out.println("4.id" + selectByName.getId());
            Integer integer1 = couponService.addCpScope(selectByName.getId(), list);
            if (integer > 0 && integer1 > 0) {
                jsonResult.setCode("1");
                jsonResult.setMsg("添加成功");
            } else {
                jsonResult.setCode("0");
                jsonResult.setMsg("添加失败");
            }
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                jsonResult.setCode("0");
                jsonResult.setMsg(e.getMessage());
            } else {
                e.printStackTrace();
            }
        }

        return jsonResult;
    }

}
